/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ebookmgr;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class AddFolderDialog extends JDialog {

    /**
     *
     */
    private static final long serialVersionUID = 2374095108446479683L;

    public AddFolderDialog(Frame owner, boolean modal) {
        super(owner, modal);

        this.setTitle(Configuration.getUIString("AddFolderDialog.Title"));
        // set size
        this.setPreferredSize(new Dimension(300, 150));
        this.setSize(new Dimension(300, 100));

        this.setLocation(EBookMgrUtil.getProperPosition(owner, this));

        // set layout
        GridBagLayout layout = new GridBagLayout();
        this.setLayout(layout);

        // add label
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.weightx = 0.5;
        JLabel folderNameLabel = new JLabel(Configuration.getUIString("AddFolderDialog.FolderName"));
        folderNameLabel.setHorizontalAlignment(JLabel.RIGHT);
        this.add(folderNameLabel, gbc);

        // add folder name text
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.weightx = 1;
        folderName = new JTextField();
        this.add(folderName, gbc);

        // add label
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        JLabel keywordLabel = new JLabel(Configuration.getUIString("AddFolderDialog.KeyWord"));
        keywordLabel.setHorizontalAlignment(JLabel.RIGHT);
        this.add(keywordLabel, gbc);

        // add keyword text
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        keyword = new JTextField();
        this.add(keyword, gbc);

        // add cancel button and event
        JButton cancel = new JButton(Configuration.getUIString("AddFolderDialog.Cancle"));
        cancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                isOK = false;
            }
        });

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        this.add(cancel, gbc);

        // add ok button and event
        JButton ok = new JButton(Configuration.getUIString("AddFolderDialog.Ok"));
        ok.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String folderNameStr = getFolderName();
                if(folderNameStr == null || folderNameStr.length() == 0) {
                    // @TODO: add error message here.
                    folderName.setFocusable(true);
                    folderName.requestFocus();
                    return ;
                }
                
                String keywordStr = getKeyword();
                if(keywordStr == null || keywordStr.length() == 0) {
                    // @TODO: add error message here.
                    keyword.setFocusable(true);
                    keyword.requestFocus();
                    return ;
                }
                setVisible(false);
                isOK = true;

            }
        });
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        this.add(ok, gbc);
    }
    JTextField folderName = null;
    JTextField keyword = null;
    private boolean isOK = false;

    /**
     * get input folder name
     * @return
     */
    public String getFolderName() {
        return this.folderName.getText();
    }

    public void setFolderName(String folderName) {
        this.folderName.setText(folderName);
    }

    /**
     * get input keyword
     * @return
     */
    public String getKeyword() {
        return this.keyword.getText();
    }

    public void setKeyword(String keyword) {
        this.keyword.setText(keyword);
    }

    /*
     * is OK clicked
     */
    public boolean isOK() {
        return this.isOK;
    }
}
